package cn.com.bluemoon.daps.model.controller.api;

import cn.com.bluemoon.daps.api.model.RemoteModelMetadataMappingService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiModel;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.model.entity.DapModelMetadataMapping;
import cn.com.bluemoon.daps.model.service.DapModelMetadataMappingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = {"FeignClient服务-模型元数据系统映射"})
@RequestMapping(DapsApiModel.API_MODEL_METADATA_MAPPING)
@BmPermission(value = "", name = "服务注册-模型元数据系统映射", ignore = true)
@RestController
@Slf4j
public class DapModelMetadataMappingApi implements RemoteModelMetadataMappingService {

    @Autowired
    private DapModelMetadataMappingService dapModelMetadataMappingService;

    @Override
    public ResultBean<Map<String, String>> getModelSystemIds(List<String> tableSystemIdList) {
        List<DapModelMetadataMapping> list;
        if(CollectionUtils.isEmpty(tableSystemIdList)){
            list = dapModelMetadataMappingService.list();
        }else {
            LambdaQueryWrapper<DapModelMetadataMapping> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(DapModelMetadataMapping::getMetadataSystemId,tableSystemIdList);
            list = dapModelMetadataMappingService.list(wrapper);
        }

        if(CollectionUtils.isEmpty(list)){
            return ResultBean.ok(Maps.newHashMap());
        }else {
            Map<String, String> map = list.stream().collect(Collectors.toMap(DapModelMetadataMapping::getMetadataSystemId, DapModelMetadataMapping::getModelSystemId));
            return  ResultBean.ok(map);
        }
    }
}
